package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.StrategyCommend;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyCommendService;
import cn.wolfcode.luowowo.article.service.ITravelCommendService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.search.query.IndexSearchQuery;
import cn.wolfcode.luowowo.search.service.IDestinationSearchService;
import cn.wolfcode.luowowo.search.service.IStrategyTemplateSearchService;
import cn.wolfcode.luowowo.search.service.ITravelSearchService;
import cn.wolfcode.luowowo.search.service.IUserInfoTemplateSearchService;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResultVO;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Reference
    private ITravelCommendService travelCommendService;
    @Reference
    private IStrategyCommendService strategyCommendService;
    @Reference
    private ITravelService travelService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IStrategyTemplateSearchService strategyTemplateSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IUserInfoTemplateSearchService userInfoTemplateSearchService;


    @RequestMapping("")
    public String index(HttpServletRequest request, @UserParam UserInfo userInfo, Model model) {
        //因为这个时候我们需要共享数据数据,所以我们要把用户对象存储到会话中
        request.getSession().setAttribute("userInfo", userInfo);
        //tcs 游记的推荐
        model.addAttribute("tcs", travelCommendService.queryCommendTop5());

        List<StrategyCommend> list = strategyCommendService.queryCommendTop5();
        //scs 攻略推荐
        model.addAttribute("scs", list.subList(0, 1));

        return "index/index";
    }

    /**
     * 首页游记推荐列表
     *
     * @param model
     * @return
     */
    @RequestMapping("/index/travelPage")
    public String travelPage(Model model, TravelQuery qo) {
        PageInfo<Travel> list = travelService.selectList(qo);
        model.addAttribute("pageInfo", list);
        return "index/travelPageTpl";
    }
    //<span style='color:red;'></span>

    /**
     * 首页es查询
     *
     * @param model
     * @param qo
     * @return
     */
    @RequestMapping("q")
    public String q(Model model, @ModelAttribute("qo") IndexSearchQuery qo) {
        //先处理0号目的地查询目的地
        if (qo.getType() == 0) {
            return searchDest(model, qo);
        } else if (qo.getType() == 3) {
            return searchUser(model, qo);
        } else if (qo.getType() == 1) {
            return searchStrategy(model, qo);
        } else if (qo.getType() == 2) {
            return searchTravel(model,qo);
        }else {
            SearchResultVO searchResultVO = new SearchResultVO();
            AggregatedPage<UserInfoTemplate> users = userInfoTemplateSearchService.selectByKeyword(qo, "nickname", "destName");
            searchResultVO.setUsers(users.getContent());
            AggregatedPage<TravelTemplate> travels = travelSearchService.selectByKeyword(qo, "title", "summary");
            searchResultVO.setTravels(travels.getContent());
            AggregatedPage<StrategyTemplate> strategys = strategyTemplateSearchService.selectByKeyword(qo, "title", "subTitle", "summary");
            searchResultVO.setStrategys(strategys.getContent());
            AggregatedPage<DestinationTemplate> dests = destinationSearchService.selectByKeyword(qo, "name", "info");
            searchResultVO.setDests(dests.getContent());
            model.addAttribute("data", searchResultVO);
            searchResultVO.setTotal(users.getTotalElements()+travels.getTotalElements()+strategys.getTotalElements()+dests.getTotalElements());
            return "index/searchAll";
        }

    }

    private String searchTravel(Model model, IndexSearchQuery qo) {
        AggregatedPage<TravelTemplate> travels = travelSearchService.selectByKeyword(qo, "title", "summary");
        model.addAttribute("page", travels);
        return "index/searchTravel";
    }

    private String searchStrategy(Model model, IndexSearchQuery qo) {
        AggregatedPage<StrategyTemplate> strategys = strategyTemplateSearchService.selectByKeyword(qo, "title", "subTitle", "summary");
        model.addAttribute("page", strategys);
        return "index/searchStrategy";
    }

    private String searchUser(Model model, IndexSearchQuery qo) {
        AggregatedPage<UserInfoTemplate> users = userInfoTemplateSearchService.selectByKeyword(qo, "nickname", "destName");
        model.addAttribute("page", users);
        return "index/searchUser";
    }

    private String searchDest(Model model, IndexSearchQuery qo) {
        DestinationTemplate dest = destinationSearchService.findByName(qo.getKeyword());
        model.addAttribute("dest", dest);
        SearchResultVO resultVO = new SearchResultVO();
        if (dest != null) {
            //查询攻略
            List<StrategyTemplate> strategys = strategyTemplateSearchService.findByDestName(qo.getKeyword());
            if (strategys.size() > 0) {
                resultVO.setStrategys(strategys);
                resultVO.setTotal(resultVO.getTotal() + strategys.size());
            }
            //查询游记
            List<TravelTemplate> travels = travelSearchService.findByDestName(qo.getKeyword());
            if (travels.size() > 0) {
                resultVO.setTravels(travels);
                resultVO.setTotal(resultVO.getTotal() + travels.size());
            }
            //查询用户
            List<UserInfoTemplate> users = userInfoTemplateSearchService.findByDestName(qo.getKeyword());
            if (users.size() > 0) {
                resultVO.setUsers(users);
                resultVO.setTotal(resultVO.getTotal() + users.size());
            }
        }
        model.addAttribute("data", resultVO);
        return "index/searchDest";
    }


}
